
// 求树的左叶子结点的和

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct TreeNode
{
    int data;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;

TreeNode* BuyTreeNode(int x)
{
    TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));
    assert(newnode);

    newnode->data = x;
    newnode->left = NULL;
    newnode->right = NULL;

    return newnode;
}

TreeNode* BuyTree()
{
    TreeNode* n1 = BuyTreeNode(1);
    TreeNode* n2 = BuyTreeNode(2);
    TreeNode* n3 = BuyTreeNode(3);
    TreeNode* n4 = BuyTreeNode(4);
    TreeNode* n5 = BuyTreeNode(5);
    TreeNode* n6 = BuyTreeNode(6);

    n1->left = n2;
    n1->right = n3;
    n2->left = n4;
    n2->right = n5;
    n3->left = n6;

    return n1;
}

void LeftLeafSum(TreeNode* root, int* sum, int flag)
{
    if (root == NULL) return;

    if (flag && root->left == NULL && root->right == NULL)
    {
        *sum += root->data;
        return;
    }

    LeftLeafSum(root->left, sum, 1);
    LeftLeafSum(root->right, sum, 0);
}

int main()
{
    TreeNode* root = BuyTree();

    int sum = 0;
    int flag = 1;
    LeftLeafSum(root, &sum, flag);

    printf("%d\n", sum);

    return 0;
}
